using System;
using System.Drawing;
using System.Collections;
using System.ComponentModel;
using System.Windows.Forms;
using System.Data;
using System.Net;
using System.IO;

namespace weather
{
	/// <summary>
	/// Summary description for tray.
	/// </summary>
	public class Tray : System.Windows.Forms.Form
	{
		private System.Windows.Forms.NotifyIcon notifyIcon1;
		private System.Windows.Forms.Timer timer1;
		private System.ComponentModel.IContainer components;
		private string temperature;

		public string Temperature
		{
			get
			{
				return temperature;
			}
		}

		public Tray()
		{
			//
			// Required for Windows Form Designer support
			//
			InitializeComponent();
			InitializeMenu();
			timer1.Start();
			getTemp();
		}

		/// <summary>
		/// Clean up any resources being used.
		/// </summary>
		protected override void Dispose( bool disposing )
		{
			notifyIcon1.Visible = false;

			if( disposing )
			{
				if(components != null)
				{
					components.Dispose();
				}
			}
			base.Dispose( disposing );
		}

		#region Windows Form Designer generated code
		/// <summary>
		/// Required method for Designer support - do not modify
		/// the contents of this method with the code editor.
		/// </summary>
		private void InitializeComponent()
		{
			this.components = new System.ComponentModel.Container();
			this.notifyIcon1 = new System.Windows.Forms.NotifyIcon(this.components);
			this.timer1 = new System.Windows.Forms.Timer(this.components);
			// 
			// notifyIcon1
			// 
			this.notifyIcon1.Text = "Boulder Weather";
			this.notifyIcon1.Visible = true;
			this.notifyIcon1.DoubleClick += new System.EventHandler(this.Icon_DoubleClick);
			// 
			// timer1
			// 
			this.timer1.Interval = 300000;
			this.timer1.Tick += new System.EventHandler(this.timer1_Tick);
			// 
			// Tray
			// 
			this.AutoScaleBaseSize = new System.Drawing.Size(5, 13);
			this.ClientSize = new System.Drawing.Size(292, 266);
			this.Name = "Tray";
			this.Opacity = 0;
			this.ShowInTaskbar = false;
			this.Text = "tray";

		}
		#endregion
		
		/// <summary>
		/// The main entry point for the application.
		/// </summary>
		[STAThread]
		static void Main() 
		{
			Application.Run(new Tray());
		}

		private void getTemp()
		{
			
				
				string url = "http://www.atd.ucar.edu/cgi-bin/weather.cgi?site=fl&units=english";
				HttpWebRequest wr = (HttpWebRequest)WebRequest.Create(url);

				HttpWebResponse ws = (HttpWebResponse)wr.GetResponse();

				Stream str = ws.GetResponseStream();
				StreamReader sr = new StreamReader(str);

				string line = " ";
				while(line.IndexOf("<th>Temperature</th>") == -1)
				{
					line = sr.ReadLine();
				}
				line = sr.ReadLine();
				
				string[] tokens = line.Split(new char[] {'>'});
				string[] token2 = tokens[1].Split(new char[] {'.'});
				temperature = token2[0];
			
				this.Text = temperature + " degrees" + "." + token2[1].Substring(0,1);
				this.notifyIcon1.Text = "Current temperature for Boulder, CO is " + temperature + "." + 
					token2[1].Substring(0,1) + " degrees";
				
				String file = "weather." + temperature + ".ico";

				try
				{
					notifyIcon1.Icon = new Icon(typeof(Tray).Assembly.GetManifestResourceStream(file));
				}
				catch(Exception e)
				{
				
				}

				notifyIcon1.Visible = true;
				
				ws.Close();
			
		
		}
		private void timer1_Tick(object sender, System.EventArgs e)
		{
			getTemp();
		}
		public void refresh()
		{
			getTemp();
		}
		private void Refresh(object sender, System.EventArgs e)
		{
			getTemp();
		}
		private void ShowWindow(object sender, System.EventArgs e)
		{
			Window window = new Window(this);
		}
		private void ShowRadar(object sender, System.EventArgs e)
		{
		   Radar radar = new Radar();
		}
		private void ShowForecast(object sender, System.EventArgs e)
		{
			Forecast forecast = new Forecast();
		}
		private void ExitControlForm(object sender, System.EventArgs e)
		{
			this.Close();
			this.Dispose();
		}
		private void Icon_DoubleClick(object sender, System.EventArgs e)
		{
			Forecast forecast = new Forecast();
		}
		private void InitializeMenu()
		{
			//Create the MenuItem objects and add them to
			//the context menu of the NotifyIcon.
			MenuItem[] mnuItems = new MenuItem[7];
			//create the menu items array
			mnuItems[0] = new MenuItem("Show Information Window...", new EventHandler(this.ShowWindow));
			mnuItems[0].DefaultItem = true;
			mnuItems[1] = new MenuItem("Radar", new EventHandler(ShowRadar));
			mnuItems[2] = new MenuItem("-");
			mnuItems[3] = new MenuItem("Refresh", new EventHandler(this.Refresh));
			mnuItems[4] = new MenuItem("Forecast", new EventHandler(ShowForecast));
			mnuItems[5] = new MenuItem("-");
			mnuItems[6] = new MenuItem("Exit", new EventHandler(this.ExitControlForm));
		
			//add the menu items to the context menu of the NotifyIcon
			ContextMenu notifyIconMenu = new ContextMenu(mnuItems);
			notifyIcon1.ContextMenu=notifyIconMenu;

		}

		
	}
}
